/**
 * 
 */
package com.fwzc.giuseppe.activity;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.util.ScreenUtil;
import com.fwzc.giuseppe.view.MyAlertDialog;
import com.gghl.view.wheelview.JudgeDate;
import com.gghl.view.wheelview.WheelMain;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * 
 * 类名称：UpdateDateTime 类描述： 创建人：何琳 创建时间：2015年6月19日 下午2:50:30
 * 
 * @version
 * 
 */
public class UpdateBirthdayActivity extends BaseActivity implements
		OnClickListener {

	private TextView txttime;
	WheelMain wheelMain;
	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	String birthday;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_edit_birthday);

		txttime = (TextView) findViewById(R.id.datetime_edt);
		txttime.setOnClickListener(this);

		initBar();
		initHeadView();
		initData();
	}

	/**
	 * 
	 */
	private void initData() {
		txttime.setText(this.getIntent().getStringExtra("birthday_value"));

	}

	/**
	 * 
	 */
	private void initHeadView() {
		actionbar_side_name.setText(this.getIntent().getStringExtra(
				"birthday_title"));
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		tv_actionbar_side_right.setVisibility(View.VISIBLE);
		tv_actionbar_side_right.setText("保存");
		tv_actionbar_side_right.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				checkSubmit();
			}
		});
	}

	private void checkSubmit() {
		birthday = txttime.getText().toString().trim();
		Intent intent = this.getIntent();
		intent.putExtra("birthday_value", birthday);
		setResult(RESULT_OK, intent);
		finish();

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.datetime_edt:
			LayoutInflater inflater = LayoutInflater
					.from(UpdateBirthdayActivity.this);
			final View timepickerview = inflater.inflate(R.layout.timepicker,
					null);
			wheelMain = new WheelMain(timepickerview);
			wheelMain.screenheight = ScreenUtil.getScreenHeight(this);
			String time1 = txttime.getText().toString();
			Calendar calendar1 = Calendar.getInstance();
			if (JudgeDate.isDate(time1, "yyyy-MM-dd")) {
				try {
					calendar1.setTime(dateFormat.parse(time1));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			int year1 = calendar1.get(Calendar.YEAR);
			int month1 = calendar1.get(Calendar.MONTH);
			int day1 = calendar1.get(Calendar.DAY_OF_MONTH);
			wheelMain.initDateTimePicker(year1, month1, day1);
			final MyAlertDialog dialog = new MyAlertDialog(
					UpdateBirthdayActivity.this).builder()
					.setTitle("出生")
					.setView(timepickerview)
					.setNegativeButton("取消", new OnClickListener() {
						@Override
						public void onClick(View v) {

						}
					});
			dialog.setPositiveButton("保存", new OnClickListener() {
				@Override
				public void onClick(View v) {
					txttime.setText(wheelMain.getTime());
				}
			});
			dialog.show();

			break;
		case R.id.iv_actionbar_side_left:
			finish();
			break;
		default:
			break;
		}

	}

}
